package cn.bjxy.javaks.store_ms.utils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import cn.bjxy.javaks.store_ms.entity.AdminEntity;
import cn.bjxy.javaks.store_ms.entity.GoodsEntity;
import cn.bjxy.javaks.store_ms.entity.LogEntity;

/**
 * 库存管理系统的工具类
 * 
 * @author long
 *
 */
public class StoreMSysUtil {

	private StoreMSysUtil() {  // 私有化构造方法，不让new当前类的对象
	}; 

	private static String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
			"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8",
			"9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
			"U", "V", "W", "X", "Y", "Z" };
	/**
	 * 生成8位UUID，数据库表填充主键使用
	 * @return
	 */
	public static String generateShortUuid() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();
	}
	
	public static String dateFormate(Date date) {
		return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
	}
	
	/**
	 * 获取一个类的代理对象
	 * @param interfaceClass 目标类实现的接口
	 * @param realObj 目标类
	 * @param invokeHandler 消息转发处理类
	 * @return 目标类的代理对象
	 */
	public static Object getProxyObj(Class interfaceClass, InvocationHandler invokeHandler) {
		return Proxy.newProxyInstance(
				interfaceClass.getClassLoader(), 
				new Class[] { interfaceClass },
				invokeHandler);
	}
	
	/**
	 * 将存放管理员账户信息的List集合转成二维数组
	 * @param adminList
	 * @return
	 */
	public static String[][] adminListToTArr(List<AdminEntity> adminList) {
		int row = adminList.size(), col = AdminEntity.class.getDeclaredFields().length;
		Iterator<AdminEntity> it = adminList.iterator();
		String[][] data = new String[row][col];
		for (int i = 0; i < row; i++) {
			AdminEntity admin = it.next();
			data[i][0] = admin.getId();
			data[i][1] = admin.getName();
			data[i][2] = admin.getPassword();
			data[i][3] = admin.isSex() + "";
			data[i][4] = admin.getLevel().toString();
			data[i][5] = dateFormate(admin.getAdd_date());
			data[i][6] = dateFormate(admin.getModify_date());
		}
		return data;
	}

	/**
	 * 将存放货物信息的List集合转成二维数组
	 * @param goodsList
	 * @return
	 */
	public static String[][]  goodsListToTwoArr(List<GoodsEntity> goodsList) {
		int row = goodsList.size(), col = GoodsEntity.class.getDeclaredFields().length;
		Iterator<GoodsEntity> it = goodsList.iterator();
		String[][] data = new String[row][col];
		for (int i = 0; i < row; i++) {
			GoodsEntity goods = it.next();
			data[i][0] = goods.getId();
			data[i][1] = goods.getName();
			data[i][2] = goods.getAll_num().toString();
			data[i][3] = goods.getCurrent_num().toString();
			data[i][4] = goods.getBuyer_name();
			data[i][5] = goods.getDescribe();
			data[i][6] = dateFormate(goods.getBuy_date());
			data[i][7] = dateFormate(goods.getExpire_date());
			data[i][8] = dateFormate(goods.getAdd_date());
			data[i][9] = dateFormate(goods.getModify_date());
		}
		return data;
	}
	
	/**
	 * 将存放货物信息的List集合转成二维数组
	 * @param logList
	 * @return
	 */
	public static String[][]  logListToTwoArr(List<LogEntity> logList) {
		int row = logList.size(), col = LogEntity.class.getDeclaredFields().length;
		Iterator<LogEntity> it = logList.iterator();
		String[][] data = new String[row][col];
		for (int i = 0; i < row; i++) {
			LogEntity log = it.next();
			data[i][0] = log.getId();
			data[i][1] = log.getAdmin_name();
			data[i][2] = log.getContent();
			data[i][3] = log.getOperate_type();
			data[i][4] = log.getOperate_obj();
			data[i][5] = log.getOperate_obj_id();
			data[i][6] = dateFormate(log.getOperate_date());
		}
		return data;
	}
}
